﻿using Biz;
using BusinessLayer.Biz;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace App
{
    public partial class FrmThongKe : Form
    {
        public FrmThongKe()
        {
            InitializeComponent();
        }
        Product product = new Product();
        List<Product> products;
        List<TonKho> tonkhos;
        private void FrmThongKe_Load(object sender, EventArgs e)
        {
            products = product.GetAll();
            LoadDsSanPham();
            LoadLoaiSPCbx();
            this.rpv.RefreshReport();
        }

        public void LoadLoaiSPCbx()
        {
            Category cat = new Category();
            List<Category> cbx;
            cbx = cat.GetAll();
            Category tatca = new Category();
            tatca.Name = "Tất cả";
            tatca.Id = -1;
            cbx.Add(tatca);
            cbxDanhMuc.ValueMember = "Id";
            cbxDanhMuc.DisplayMember = "Name";
            cbxDanhMuc.DataSource = cbx;
            cbxDanhMuc.SelectedItem = tatca;


        }
        public void LoadDsSanPham()
        {
            lstDsSanPham.Items.Clear();
            foreach (var p in products)
            {
                lstDsSanPham.Items.Add(p);
            }
        }
        public void LoadDsTonKho()
        {
            lstDsNhap.Items.Clear();
            
            foreach (var t in tonkhos)
            {
                ListViewItem lvi = new ListViewItem(t.product.ToString());
                ListViewItem.ListViewSubItem sit = new ListViewItem.ListViewSubItem(lvi, t.TonDau.ToString());
                ListViewItem.ListViewSubItem sit1 = new ListViewItem.ListViewSubItem(lvi, t.Nhap.ToString());
                ListViewItem.ListViewSubItem sit2 = new ListViewItem.ListViewSubItem(lvi, t.Xuat.ToString());
                ListViewItem.ListViewSubItem sit3 = new ListViewItem.ListViewSubItem(lvi, t.TonCuoi.ToString());
                lvi.SubItems.Add(sit);
                lvi.SubItems.Add(sit1);
                lvi.SubItems.Add(sit2);
                lvi.SubItems.Add(sit3);
                lvi.Tag = t;
                lstDsNhap.Items.Add(lvi);
            }
        }
        private void btnTkTatCa_Click(object sender, EventArgs e)
        {
            DateTime from = DateTime.Parse(string.Format("{0} 00:00:00", String.Format("{0:MM/dd/yyyy}", dtpNgayDau.Value)));
            DateTime to = DateTime.Parse(string.Format("{0} 23:59:59", String.Format("{0:MM/dd/yyyy}", dtpNgayCuoi.Value)));
            if (lstDsSanPham.Items.Count <= 0)
            {
                MessageBox.Show(this, "Không có dữ liệu!", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            tonkhos = new List<TonKho>();
            Unsold unsold = new Unsold();
            foreach (Object item in lstDsSanPham.Items)
            {
                Product product = (Product)item;
                tonkhos.Add(unsold.GetByProduct(product,from,to));
            }
            LoadDsTonKho();
        }

        private void btnTkDaChon_Click(object sender, EventArgs e)
        {
            DateTime from = DateTime.Parse(string.Format("{0} 00:00:00", String.Format("{0:MM/dd/yyyy}", dtpNgayDau.Value)));
            DateTime to = DateTime.Parse(string.Format("{0} 23:59:59", String.Format("{0:MM/dd/yyyy}", dtpNgayCuoi.Value)));
            Unsold unsold = new Unsold();
            tonkhos = new List<TonKho>();
            if (lstDsSanPham.SelectedItems.Count == 0)
            {
                MessageBox.Show(this, "Chưa chọn sản phẩm!", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            //sanphams = new List<SANPHAM>();
            foreach (Object selecteditem in lstDsSanPham.SelectedItems)
            {
                tonkhos.Add(unsold.GetByProduct((Product)selecteditem, from, to));
            }
            LoadDsTonKho();
        }

        private void cbxDanhMuc_SelectedIndexChanged(object sender, EventArgs e)
        {
            int id = int.Parse(cbxDanhMuc.SelectedValue.ToString());
            products = product.SearchByCondition("", -1, -1, id, -1);
            LoadDsSanPham();
        }

        private void dtpNgayDau_ValueChanged(object sender, EventArgs e)
        {
            dtpNgayCuoi.MinDate = dtpNgayDau.Value;
        }

        private void dtpNgayCuoi_ValueChanged(object sender, EventArgs e)
        {
            dtpNgayDau.MaxDate = dtpNgayCuoi.Value;
        }

        private void BtnDoIt_Click(object sender, EventArgs e)
        {
            danhThu.EnforceConstraints = false;
            this.reportTableAdapter.Fill(this.danhThu.Report, dtFrom.Value, dtTo.Value);
            //rpv.LocalReport.SetParameters(new Microsoft.Reporting.WinForms.ReportParameter("StartDate", dtFrom.Value.ToString("dd/MM/yyyy")));
            //rpv.LocalReport.SetParameters(new Microsoft.Reporting.WinForms.ReportParameter("EndDate", dtTo.Value.ToString("dd/MM/yyyy")));
            rpv.RefreshReport();
        }
    }
}
